/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.bean.impl;

import javax.activation.FileTypeMap;

import com.genia.toolbox.basics.bean.ContentTypeChecker;
import com.genia.toolbox.basics.bean.DataContainer;
import com.genia.toolbox.basics.exception.technical.TechnicalException;

/**
 * base implementation of {@link DataContainer}.
 */
public abstract class AbstractDataContainer
    implements DataContainer
{
  /**
   * whether to recompute a correct content type.
   */
  private transient String computedContentType = null;

  /**
   * the type of the file.
   */
  private String contentType;

  /**
   * the name of the file.
   */
  private String name;



  /**
   * getter for the contentType property.
   * 
   * @return the contentType
   */
  public String getContentType()
  {
    synchronized (this) {

      if (computedContentType == null) {
        final FileTypeMap fileTypeMap = FileTypeMap.getDefaultFileTypeMap();
        computedContentType = contentType;
        if (computedContentType == null) {
          computedContentType = fileTypeMap.getContentType(getName());
        }
        if (fileTypeMap instanceof ContentTypeChecker) {
          ContentTypeChecker contentTypeChecker = (ContentTypeChecker) fileTypeMap;
          if (!contentTypeChecker.isResolved(computedContentType) || !contentTypeChecker.isStandard(computedContentType)) {
            String newContentType = fileTypeMap.getContentType(getName());
            if (contentTypeChecker.isResolved(newContentType)) {
              computedContentType = newContentType;
            }
          }
        }
      }
    }
    return computedContentType;
  }



  /**
   * getter for the name property.
   * 
   * @return the name
   */
  public String getName()
  {
    return name;
  }



  /**
   * setter for the contentType property.
   * 
   * @param contentType
   *          the contentType to set
   * @throws TechnicalException
   *           when an error occurred
   */
  public void setContentType(String contentType)
      throws TechnicalException
  {
    this.contentType = contentType;
    computedContentType = null;
  }



  /**
   * setter for the name property.
   * 
   * @param name
   *          the name to set
   */
  public void setName(String name)
  {
    this.name = name;
    computedContentType = null;
  }

}
